import requests
import json
from 业务效能写入表格 import write_to_excel
import 时间戳更新

region = {
    "遵义市": "520300",
    "红花岗区": "520302",
    "汇川区": "520303",
    "播州区": "520321",
    "桐梓县": "520322",
    "绥阳县": "520323",
    "正安县": "520324",
    "道真县": "520325",
    "务川县": "520326",
    "凤冈县": "520327",
    "湄潭县": "520328",
    "余庆县": "520329",
    "习水县": "520330",
    "赤水市": "520381",
    "仁怀市": "520382",
    "新蒲新区": "520383",

}
# 时间戳更新
for key, value in region.items():
    with open('cookie.txt', 'r') as file:
        headers = json.load(file)
    url = 'http://59.215.208.54:91/sysaction.do'
    params = {
        "ClassName": "com.hyysoft.smart.action.call.CallGetNumRecAction.GetCallYWBJCount",
        "DB_Conn": "SMARTJH",
        "hall_id": value,
        "page": "1",
        "limit": "10",
        "getnum_date": "2025-10-01 ~ 2025-10-31",
        "yw_name": ""
    }
    try:
        response = requests.get(url, params=params, headers=headers).json()
        formatted_response = json.dumps(response, indent=4, ensure_ascii=False)

    except requests.exceptions.JSONDecodeError:
        print(f'请求 {key} 返回内容无法解析为 JSON，请检查请求和响应。')
        continue
    data = []
    for i in response['data']:
        data.append(
            [i['yw_number'], i['yw_name'], i['getnum_count'], i['getnum_nonum'], i['getnum_yesnum'], i['getnum_blz'],
             i['getnum_pjbl'], i['getnum_ckpjbl'], i['getnum_pjdd']])
        # print(f"业务号{i['yw_number']},业务名{i['yw_name']},取号数{i['getnum_count']} , 未呼数{i['getnum_nonum']},已呼叫{i['getnum_yesnum']},办理中{i['getnum_blz']},群众平均办理时长(分钟){i['getnum_pjbl']},窗口平均办理时长(分钟){i['getnum_ckpjbl']},平均等待时长(分钟){i['getnum_pjdd']}")
    write_to_excel(data, key)
    print(f"{key}写入成功")
